/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun;

import com.norbl.util.s3.*;
import com.norbl.util.*;
import com.xerox.amazonws.ec2.*;
import java.io.*;
import org.jets3t.service.S3ServiceException;

/**
 *
 * @author Barnet Wagman
 */
public class GetJarsFromS3 {

    static File userDataFile;

    public GetJarsFromS3() {}

    public static void main(String[] argv) {
        try {
            if ( (argv != null) && (argv.length == 1) ) userDataFile = new File(argv[0]);
            else userDataFile = null;

            GetJarsFromS3 gjs = new GetJarsFromS3();           
            gjs.getJars();
        }
        catch(Throwable x) {
            handleThrowable(x);
            System.exit(0); // Any exception is terminal.
        }
    }

    public void getJars() throws IOException {
        try {
            String userData = null;
            if ( (userDataFile == null) || !userDataFile.exists() )
                userData = EC2Utils.getInstanceUserdata();
            else userData = getUserDataFromFile();
            NodeParams params = new NodeParams(userData);
            S3Ops s3Ops = new S3Ops(params.awsAccessID,params.awsSecretKey,
                                    Constants.S3_RETRY_INTERVAL_MILLIS_DEFAULT,
                                    Constants.S3_MAX_WAIT_MILLIS_DEFAULT);
            File jarDir = new File(params.jarDirPath);
            s3Ops.getJarsFromS3(jarDir,params.jarBucketName,params.jarS3KeyPrefix,
                                userDataFromFile());
        }
        catch(S3ServiceException sx) { throw new S3AccessException(sx); }
    }

    public void getJars(String userData) throws IOException {
        try {
            NodeParams params = new NodeParams(userData);
            S3Ops s3Ops = new S3Ops(params.awsAccessID,params.awsSecretKey,
                                    Constants.S3_RETRY_INTERVAL_MILLIS_DEFAULT,
                                    Constants.S3_MAX_WAIT_MILLIS_DEFAULT);
            File jarDir = new File(params.jarDirPath);
            s3Ops.getJarsFromS3(jarDir,params.jarBucketName,params.jarS3KeyPrefix,
                                userDataFromFile());
        }
        catch(S3ServiceException sx) { throw new S3AccessException(sx); }
    }

    static void handleThrowable(Throwable x) {
        /* D */ System.out.println(x);
    }

        /** Used for test on a system that is NOT an ec2 instance.
         *
         * @return
         */
    static String getUserDataFromFile() {
        System.out.println("Getting user data from file=" + userDataFile.getPath());
        return( FileUtil.readFile(userDataFile) );
    }

    static boolean userDataFromFile() { return(userDataFile != null); }
}
